<template>
  <div class="content-outer">
    <div class="content-head">
      <div class="search-form">
        <div class="search-item">
          <div class="item-label">小程序名称</div>
          <div class="item-content">
            <el-select
              v-model="formData.mini_app_id"
              filterable
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in optionsList.miniApp"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                style="display: flex; align-items: center;justify-content: space-between;"
              >
                <span style="float: left">{{ item.name }}</span>
                <svg-icon
                  v-if="item.app_type==1"
                  style="float: right;"
                  icon-class="miniapp"
                  class="imageIcon"
                />
                <svg-icon
                  v-else
                  style="float: right;"
                  icon-class="douyin"
                  class="imageIcon"
                />
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="search-item">
          <BiDatePicker
            v-model="formData.date"
            size="small"
            type="daterange"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            range-separator="~"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
          />
        </div>
        <div class="search-item">
          <el-button
            :loading="loading"
            style="margin-left: 10px"
            type="primary"
            @click="onQuery"
          >查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </div>
      </div>
    </div>

    <Block class="content-table">
      <div class="action-box">
        <div class="left"></div>
        <div class="right">
          <el-button @click="onBiDrawer({},'新增')">新增</el-button>
        </div>
      </div>
      <div class="table-table">
        <BiTable
          :loading="loading"
          :data="tableData"
          size="medium"
          :column="tableColums"
          fixed="sticky"
          fixed-offset="50"
          pagination
          layout="total, prev, pager, next"
          :auto-scroll="false"
          :total="page_info.total"
          :page.sync="page_info.page"
          :limit.sync="page_info.page_size"
          @pagination="handlePageChange"
        >
          <template v-slot:zip="{row}">
            <div>
              <el-button
                v-if="row.status == 1"
                type="text"
                size="medium"
                @click="onBiDrawer(row,'编辑')"
              >编辑</el-button>
              <el-button
                type="text"
                size="medium"
                @click="onBiDrawer(row,'查看')"
              >查看</el-button>
              <el-button
                type="text"
                size="medium"
                @click="onDelete(row,'删除')"
              >删除</el-button>
            </div>
          </template>
        </BiTable>
      </div>
    </Block>
    <BiDrawer
      v-model="drawer.visible"
      :data="drawer.row"
      :drawer-title="drawer.title"
      :component="SetSubscribe"
      drawer-size="750px"
      @ok="onSubmit"
    ></BiDrawer>

  </div>
</template>

<script>
import SetSubscribe from '../component/setSubscribe.vue'
import { getSubscribeList, subscribeDel } from '@/api/subscribe'
import { commonData, miniAppList } from '@/api/common'
export default {
  data() {
    return {
      loading: false,
      formData: {
        mini_app_id: '',
        date: []
      },
      optionsList: {
        miniApp: []
      },
      page_info: {
        total: 0,
        page: 1,
        page_size: 20
      },
      tableData: [],
      tableColums: [
        {
          label: '任务名称',
          prop: 'title',
          width: '150'
        },
        {
          label: '小程序名称',
          prop: 'mini_app_name',
          render: (h, { row }) => {
            if (row.app_type == 1) {
              return (
                <p>
                  {row.mini_app_name}
                  <svg-icon icon-class='miniapp' class='imageIcon' />
                </p>
              )
            } else if (row.app_type == 2) {
              return (
                <p>
                  {row.mini_app_name} <svg-icon icon-class='douyin' class='imageIcon' />
                </p>
              )
            } else {
              return <p>{row.mini_app_name}</p>
            }
          }
        },
        {
          label: '模板标题',
          prop: 'template_title'
          // slotScope: true
        },
        {
          label: '创建者',
          prop: 'creator'
          // slotScope: true
        },
        {
          label: '发送状态',
          prop: 'status',
          render(h, { row }) {
            return <div>{row.status == 1 ? <el-tag type='warning'>待发送</el-tag> : row.status == 2 ? <el-tag>发送中</el-tag> : <el-tag type='success'>发送成功</el-tag>}</div>
          }
        },
        {
          label: '计划发送时间',
          prop: 'send_time'
          // slotScope: true
        },
        {
          label: '发送完成时间',
          prop: 'finish_time'
          // slotScope: true
        },
        {
          label: '发送成功人数',
          prop: 'success_num'
          // slotScope: true
        },
        {
          label: '操作',
          prop: 'zip',
          slotScope: true
        }
      ],
      SetSubscribe: SetSubscribe,
      drawer: {
        visible: false,
        row: {},
        title: ''
      }
    }
  },
  mounted() {
    this.getMinApp()
    this.onSubmit()
  },
  methods: {
    onQuery() {
      this.page_info.page = 1
      this.onSubmit()
    },
    async getMinApp() {
      const res = await miniAppList()
      if (res.status_code === 1) {
        this.optionsList.miniApp = res.data
      }
    },
    handleReset() {
      this.formData = {
        mini_app_id: '',
        date: ''
      }
    },
    async onSubmit() {
      this.loading = true
      const params = {
        mini_app_id: this.formData.mini_app_id,
        send_start_date: this.formData.date ? this.formData.date[0] : '',
        send_end_date: this.formData.date ? this.formData.date[1] : '',
        page: this.page_info.page,
        page_size: this.page_info.page_size
      }
      const res = await getSubscribeList(params)
      if (res.status_code === 1) {
        this.tableData = res.data.data
        this.page_info = res.data.page_info
      }
      this.loading = false
    },
    handlePageChange(value) {
      this.page_info.page = value.page
      this.onSubmit()
    },
    onBiDrawer(row, type) {
      if (type === '编辑') {
        this.drawer = {
          visible: true,
          row: {
            ...row,
            code: 1
          },
          title: type
        }
      } else if (type === '查看') {
        this.drawer = {
          visible: true,
          row: {
            ...row,
            code: 2
          },
          title: type
        }
      } else if (type === '新增') {
        this.drawer = {
          visible: true,
          row: {},
          title: type
        }
      }
    },
    async onDelete(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          subscribeDel({
            id: row.id
          }).then((res) => {
            if (res.status_code === 1) {
              this.$message.success(res.msg)
              this.onSubmit()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.content-outer {
  padding-top: 10px;
  width: 100%;
  .content-head {
    .search-item {
      // padding-bottom: 0px;
    }
  }
  .content-table {
    background-color: white;
  }
}
.content-outer .content-head {
  width: 100%;
  background-color: white;
  color: #333333;
  font-size: 14px;
  margin-bottom: 10px;
  padding: 20px 20px 0 20px;
}
.search-form .search-item {
  min-width: 330px !important;
  padding-right: 20px;
  padding-bottom: 20px;
  margin-bottom: 0;
  margin-right: 0;
  margin-left: 0;
  margin-top: 0;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-flex: 0;
  -ms-flex: 0 0 auto;
  flex: 0 0 auto;
}
</style>
